package com.cy.aircompressor.main.home.dialog;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;

import androidx.fragment.app.FragmentManager;

import com.cy.aircompressor.config.AirConfig;
import com.cy.aircompressor.databinding.DialogModifyPwdLayoutBinding;
import com.cy.aircompressor.utils.MD5;
import com.drug.lib.util.ToastUtil;
import com.drug.lib.view.BaseDialog;
import com.jakewharton.rxbinding2.view.RxView;
import com.net.model.base.fragment.BaseVBDialogFragment;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class ModifyPwdDialog extends BaseVBDialogFragment<DialogModifyPwdLayoutBinding> {

    public static ModifyPwdDialog showDialogFragment(FragmentManager fragmentManager, String tag) {
        Bundle args = new Bundle();
        ModifyPwdDialog fragment = new ModifyPwdDialog();
        fragment.setArguments(args);
        fragment.show(fragmentManager, tag);
        return fragment;
    }

    @Override
    protected DialogModifyPwdLayoutBinding getViewBinding() {
        return DialogModifyPwdLayoutBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initialize() {
        RxView.clicks(mBinding.showPwd01)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    //密码可见
                    if (!mBinding.showPwd01.isSelected()) {
                        mBinding.showPwd01.setSelected(true);
                        mBinding.oldPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    }
                    //密码不可见
                    else {
                        mBinding.showPwd01.setSelected(false);
                        mBinding.oldPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    }
                });
        RxView.clicks(mBinding.showPwd02)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    //密码可见
                    if (!mBinding.showPwd02.isSelected()) {
                        mBinding.showPwd02.setSelected(true);
                        mBinding.newPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    }
                    //密码不可见
                    else {
                        mBinding.showPwd02.setSelected(false);
                        mBinding.newPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    }
                });
        RxView.clicks(mBinding.showPwd03)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    //密码可见
                    if (!mBinding.showPwd03.isSelected()) {
                        mBinding.showPwd03.setSelected(true);
                        mBinding.againNewPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    }
                    //密码不可见
                    else {
                        mBinding.showPwd03.setSelected(false);
                        mBinding.againNewPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    }
                });
        RxView.clicks(mBinding.closeBtn)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    dismiss();
                });
//        RxView.clicks(mBinding.cancelButton)
//                .throttleFirst(500, TimeUnit.MILLISECONDS)
//                .subscribe(o -> {
//                    dismiss();
//                });
        RxView.clicks(mBinding.verifyButton)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    String oldPwd = mBinding.oldPwd.getText().toString().trim();
                    String newPwd = mBinding.newPwd.getText().toString().trim();
                    String againNewPwd = mBinding.againNewPwd.getText().toString().trim();
                    if (TextUtils.isEmpty(oldPwd)) {
                        ToastUtil.show("请输入旧密码");
                        return;
                    }

                    if (TextUtils.isEmpty(newPwd)) {
                        ToastUtil.show("请输入新密码");
                        return;
                    }
                    if (TextUtils.isEmpty(againNewPwd)) {
                        ToastUtil.show("请输入确认密码");
                        return;
                    }
                    if (!TextUtils.equals(newPwd, againNewPwd)) {
                        ToastUtil.show("两次输入新密码不一致");
                        return;
                    }
                    if (newPwd.length() < 8) {
                        ToastUtil.show("密码长度不得小于8位");
                        return;
                    }
                    Map<String, String> map = new HashMap<>();
                    map.put("id", AirConfig.get().userInfo.getId());
                    map.put("newPwd1", MD5.encrypt2ToMD5(newPwd));
                    map.put("newPwd2", MD5.encrypt2ToMD5(againNewPwd));
                    map.put("oldPwd", MD5.encrypt2ToMD5(oldPwd));
                    if (modifyPwdCallBack != null)
                        modifyPwdCallBack.result(map);
                });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void requestNetwork() {

    }
    ModifyPwdCallBack modifyPwdCallBack;

    public void setModifyPwdCallBack(ModifyPwdCallBack modifyPwdCallBack) {
        this.modifyPwdCallBack = modifyPwdCallBack;
    }

    public interface ModifyPwdCallBack {
        void result(Object object);
    }
}
